<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AdAction
 *
 * @author Administrator
 */
import('@.Filter.SecurityAction');

import('@.Service.UserService');
import('@.Service.AppService');
import('@.Service.TypeService');
import('@.Service.FileService');
import('@.Util.ConstantUtil');
import('@.Service.NewsService');
import('@.Service.AppTopService');
import('@.Service.AdService');
import('@.Service.SubjectService');
import('@.Service.MobileService');
import('ORG.Util.Page');
import('ORG.Net.Http');
vendor('Snoopy.Snoopy#class');
vendor('simplehtmldom.simple_html_dom');

class AdAction extends SecurityAction {

    /**
     * 获取广告列表
     */
    function ad() {

//        $location = $this->getAdLocation();

        $adService = new AdService();
        $count = $adService->countAd();
        $page = new Page($count, 10);
        $show = $page->show();
        $list = $adService->listAd($page->firstRow, $page->listRows);


        $this->assign('show', $show);
        $this->assign('list', $list);

        $this->assign('content', 'ad');
        $this->display('Admin:global');
    }

    /**
     * ad add
     */
    function adAdd() {

        $this->assign('content', 'adAdd');
        $this->display('Admin:global');
    }

    function adSave() {
        $ad = D('ad')->create();
        $adService = new AdService();
        $ad['enabled'] = 1;

        if ('index_logo' == $ad['location'] || 'top' == $ad['location'] || 'content_logo' == $ad['location']
                || 'scroll1' == $ad['location'] || 'scroll2' == $ad['location'] || 'scroll3' == $ad['location'] || 'content_bottom' == $ad['location']) {
            $a = $adService->findAdByLocation($ad['location']);
            if (empty($a)) {
                $adService->insert($ad);
            } else {
                $ad['id'] = $a['id'];
                //删除原来关联的文件
                $fileService = new FileService();
                $fileService->deleteUploadFile($a['img_url']);

                //更新数据表
                $adService->save($ad);
            }
        } else {
            $adService->insert($ad);
        }

        //清空缓存
        F(ConstantUtil::$AD_CONTENT, null);
        F(ConstantUtil::$AD_INDEX, null);
        F(ConstantUtil::$INDEX_LOGO, null);
        F(ConstantUtil::$TOP_LOGO, null);

        $this->redirect('Ad/ad');
    }

    /**
     * 更新广告点击率
     */
    function countAdClickRate() {
        $id = $_POST['id'];
        $adService = new AdService();
        if (!empty($id) && !is_null($ad = $adService->getAdById($id))) {
            $ad['see_num'] += 1;

            $adService->save($ad);
        }

        $this->ajaxReturn();
    }

    /**
     * ad del
     */
    function adDel() {
        $id = $_POST['id'];
        if (!empty($id)) {
            $adService = new AdService();
            $adService->delete($id);

            //清空缓存
            F(ConstantUtil::$AD_CONTENT, null);
            F(ConstantUtil::$AD_INDEX, null);
        }

        $this->redirect('Ad/ad');
    }

    private function getAdLocation() {
        $location = $_POST['location'];
        if (empty($location)) {
            return 'index';
        }

        return $location;
    }

}

?>
